package reznic.ga.tracker.objects;

import java.io.IOException;
import reznic.ga.tracker.utils.StringUtils;

/**
 *
 * @author Reznic Alexandru <reznic.a@gmail.com>
 */
public class TrackEvent {

    private final String category;
    private final String action;
    private String label = null;
    private int value = -1111;
    private String appName;

    /**
     * &ec=video // Event Category. Required.<br>
     * &ea=play // Event Action. Required.<br>
     * &el=holiday // Event label.<br>
     * &ev=300 // Event value.<br>
     */
    public TrackEvent(String category, String action) {
        this.category = category;
        this.action = action;
    }

    public String buildURL() throws IOException {
        StringBuilder buf = new StringBuilder();
        if (StringUtils.isBlank(category) || StringUtils.isBlank(action)) {
            throw new NullPointerException("category and action is Required");
        }
        buf.append("&ec=").append(StringUtils.getURLString(category));
        buf.append("&ea=").append(StringUtils.getURLString(action));

        if (StringUtils.isNotBlank(label)) {
            buf.append("&el=").append(StringUtils.getURLString(label));
        }
        if (value != -1111) {
            buf.append("&ev=").append(value);
        }
        if (StringUtils.isNotBlank(appName)) {
            buf.append("&an=").append(StringUtils.getURLString(appName));
        }
        return buf.toString();
    }

    /**
     * for mobile events
     *
     * @param appName
     */
    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getAppName() {
        return appName;
    }

    public String getAction() {
        return action;
    }

    public String getCategory() {
        return category;
    }

    public String getLabel() {
        return label;
    }

    public int getValue() {
        return value;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public void setValue(int value) {
        this.value = value;
    }
}
